package com.example.jksj.bookmanagement.pro.message.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.jksj.bookmanagement.Base.BaseApplication;
import com.example.jksj.bookmanagement.R;
import com.example.jksj.bookmanagement.bean.LendMessageBean;
import com.example.jksj.bookmanagement.utils.L;
import com.example.jksj.bookmanagement.utils.ShareUtils;

import org.xutils.ex.DbException;

import java.util.List;

import q.rorbin.badgeview.Badge;
import q.rorbin.badgeview.QBadgeView;

public class LendMessageAdapter extends RecyclerView.Adapter<LendMessageAdapter.ViewHolder> {
    private List<LendMessageBean> lendMessageBeanList;
    private Context context;
    private  QBadgeView qBadgeView;

    /**
     * 构造方法
     */
    public LendMessageAdapter(List<LendMessageBean> lendMessageBeanList, Context context) {
        this.lendMessageBeanList = lendMessageBeanList;
        this.context = context;
    }

    /**
     * 创建ViewHolder
     * @param parent
     * @param viewType
     * @return
     */
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message,parent,false);
        final ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    /**
     * 绑定ViewHolder，每加载一项都会调用一次
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        Log.i("test","onBindViewHolder");
        holder.lendDate.setText(lendMessageBeanList.get(position).getLendDate());

        initCircular(holder.tv_system_messageItem);

        qBadgeView.setShowShadow(false);

        qBadgeView.setOnDragStateChangedListener(new Badge.OnDragStateChangedListener() {
            @Override
            public void onDragStateChanged(int dragState, Badge badge, View targetView) {
                //qBadgeView.setShowShadow(false);
                //ShareUtils.putBoolean("isCircular_" + position, false);
            }
        });
    }

    /**
     * 初始化消息圆点
     */
    private void initCircular(TextView textView ){
        qBadgeView = new QBadgeView(context);
        qBadgeView.bindTarget(textView);
        //设置消息圆点的文字
        qBadgeView.setBadgeText("1");
        qBadgeView.setBadgeGravity(Gravity.CENTER | Gravity.END);
        qBadgeView.setGravityOffset(8, 1, true);
        qBadgeView.setBadgeTextSize(12, true);
    }


    /**
     * 获取项数量
     * @return
     */
    @Override
    public int getItemCount() {
        return lendMessageBeanList.size();
    }

    /**
     * RecyclerView的持有者类
     */
    static class ViewHolder extends RecyclerView.ViewHolder{
        TextView lendDate;
        TextView tv_system_messageItem;

        public ViewHolder(View view){
            super(view);
            lendDate = view.findViewById(R.id.tv_systemDate_messageItem);
            tv_system_messageItem = view.findViewById(R.id.tv_system_messageItem);
        }
    }

}
